#include "StdAfx.h"
#include "SimpleTimeParser.h"


CSimpleDateTimeParser::CSimpleDateTimeParser(void)
	: _sstr_inp(std::string())
	, _sstr_outp(std::string())
	, _text_pattern(_T(""))
	, _locale(_T("Russian"))
{
	ZeroMemory(&_tm_struct, sizeof(_tm_struct));
	
}

CSimpleDateTimeParser::~CSimpleDateTimeParser(void)
{
}

BOOL CSimpleDateTimeParser::set_text_pattern( const std::string& str )
{
	_text_pattern = str;
	return !_text_pattern.empty();
}

BOOL CSimpleDateTimeParser::parse_date_time_string( const std::string& str )
{
	if (TRUE == str.empty())
		return FALSE;

	_sstr_inp.str(str);

	std::use_facet<std::time_get<CHAR> >(_locale).get(_sstr_inp.rdbuf(),
															NULL,
															_sstr_inp,
															_io_state,
															&_tm_struct,
															_text_pattern.c_str(),
															_text_pattern.c_str() + _text_pattern.length());
	return (std::ios_base::failbit != _io_state);
}

void CSimpleDateTimeParser::set_datetime_struct( const tm& tm_struct )
{
	_tm_struct = tm_struct;
}

std::string CSimpleDateTimeParser::format_date_time()
{
	if (TRUE == _text_pattern.empty())
		return _T("");

	_sstr_outp.str(std::string());

	std::use_facet<std::time_put<char> >(_locale).put(_sstr_outp.rdbuf(),
													_sstr_outp,
													_T(' '),
													&_tm_struct,
													_text_pattern.c_str(),
													_text_pattern.c_str() + _text_pattern.length());
	return _sstr_outp.str();
}
